{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "import numpy as np\n",
    "import sys\n",
    "import os\n",
    "from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\n",
    "import cPickle as pickle\n",
    "import re\n",
    "from scipy import linalg\n",
    "import scipy.ndimage as ndi\n",
    "from six.moves import range\n",
    "from keras import backend as K\n",
    "from PIL import Image\n",
    "import threading\n",
    "import warnings\n",
    "import theano"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_data():\n",
    "    dirname='/Users/rwa56/Downloads/tid2013/'\n",
    "    distortion='distorted_images/'\n",
    "    reference='reference_images/'\n",
    "    score='mos.txt'\n",
    "\n",
    "    size= 128, 128\n",
    "\n",
    "     \n",
    "    DistortLabel = np.zeros((3000,3), dtype='float32')\n",
    "    RefLabel = np.zeros((25,1), dtype='float32')\n",
    "    label = [0, 0, 0]\n",
    "    \n",
    "    DistortImg = np.zeros((3000, 96,128,3), dtype='uint8')\n",
    "    RefImg = np.zeros((25,96,128,3), dtype='uint8')\n",
    "    i = 0\n",
    "\n",
    "    for root, dirs, filenames in os.walk(dirname+distortion):\n",
    "        for imgfile in filenames:\n",
    "            \n",
    "            img = Image.open(root+imgfile, 'r')\n",
    "            #print img.getbands()\n",
    "            #x = img_to_array(img)\n",
    "            img.thumbnail(size, Image.ANTIALIAS)\n",
    "\n",
    "            DistortImg[i,:,:,:] = np.asarray(img, dtype='float32')\n",
    "            #x = x.reshape((1,) + x.shape)\n",
    "            #print 'The imgfile is' +imgfile\n",
    "        #    x = 0.2126 * x0[:,:,0] + 0.7152 * x0[:,:,1] + 0.0722 * x0[:,:,2]\n",
    "            #print  x.shape\n",
    "            \n",
    "            if(len(imgfile) < 12):\n",
    "                print 'The file: ' + imgfile\n",
    "            DistortLabel[i,0] = int(imgfile[1:3])\n",
    "            DistortLabel[i,1] = int(imgfile[4:6])\n",
    "            DistortLabel[i,2] = int(imgfile[7])\n",
    "            \n",
    "            i = i + 1\n",
    "            #print label\n",
    "            \n",
    "\n",
    "    j = 0\n",
    "    \n",
    "    for root, dirs, filenames in os.walk(dirname+reference):\n",
    "        for imgfile in filenames:\n",
    "            #print filename\n",
    "            img = Image.open(root+imgfile, 'r')\n",
    "            #print img.getbands()\n",
    "            #x = img_to_array(img)\n",
    "            img.thumbnail(size, Image.ANTIALIAS)\n",
    "\n",
    "            RefImg[j,:,:,:] = np.asarray(img, dtype='float32')\n",
    "            #x = x.reshape((1,) + x.shape)\n",
    "            #print 'The imgfile is' +imgfile\n",
    "    #        x = 0.2126 * x0[:,:,0] + 0.7152 * x0[:,:,1] + 0.0722 * x0[:,:,2]\n",
    "          \n",
    "            RefLabel[j] = int(imgfile[1:3])\n",
    "            #RefLabel.append(label)\n",
    "            #print label\n",
    "            j = j + 1\n",
    "\n",
    "\n",
    "    ScoreLabel = open(dirname+score).read().splitlines()\n",
    "    ScoreLabel = map(float, ScoreLabel)\n",
    "    #print ScoreLabel\n",
    "\n",
    "    #print DistortImg[1]\n",
    "\n",
    "    return DistortImg, DistortLabel, RefImg, RefLabel, ScoreLabel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "DistortImg, DistortLabel, RefImg, RefLabel, ScoreLabel = load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(96, 128, 3)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DistortImg[3].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
